package com.xjw.dao.util;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.xjw.dao.annotation.DbColumn;
import com.xjw.dao.annotation.Id;
import com.xjw.dao.annotation.NotDbColumn;
import com.xjw.dao.annotation.Table;

/**      
 * 项目名称：spring-project  
 * 实现功能：  
 * 类名称：ClassUtil   
 * 版权所有：
 * 创建人：徐纪伟 
 * E-mail: 289045706@qq.com
 * 创建时间：2017年1月12日 下午3:50:17   	
 * 修改人：   
 * 修改时间：   
 * 修改备注：   
 * @version    
 */
public class ClassUtil {
	
	
	public static void setPara(PreparedStatement ps, Object value, int columnIndex)
		    throws Exception{
		    if (value != null) {
		      Class valueType = value.getClass();
		      if (valueType.equals(String.class))
		        ps.setString(columnIndex, value.toString());
		      else if ((valueType.equals(Integer.TYPE)) || (valueType.equals(Integer.class)))
		        ps.setInt(columnIndex, Integer.parseInt(value.toString(), 10));
		      else if ((valueType.equals(Long.TYPE)) || (valueType.equals(Long.class)))
		        ps.setLong(columnIndex, Long.parseLong(value.toString()));
		      else if ((valueType.equals(Short.TYPE)) || (valueType.equals(Short.class)))
		        ps.setShort(columnIndex, Short.parseShort(value.toString()));
		      else if (valueType.equals(java.util.Date.class))
		        ps.setTimestamp(columnIndex, new Timestamp(((java.util.Date)value).getTime()));
		      else if ((valueType.equals(Boolean.TYPE)) || (valueType.equals(Boolean.class)))
		        ps.setBoolean(columnIndex, Boolean.parseBoolean(value.toString()));
		      else if ((valueType.equals(Double.TYPE)) || (valueType.equals(Double.class)))
		        ps.setDouble(columnIndex, Double.parseDouble(value.toString()));
		      else if ((valueType.equals(Float.TYPE)) || (valueType.equals(Float.class)))
		        ps.setFloat(columnIndex, Float.parseFloat(value.toString()));
		      else if ((valueType.equals(Byte.TYPE)) || (valueType.equals(Byte.class)))
		        ps.setByte(columnIndex, Byte.parseByte(value.toString()));
		      else if (valueType.equals(BigDecimal.class))
		        ps.setBigDecimal(columnIndex, new BigDecimal(value.toString()));
		      else if (valueType.equals(Timestamp.class))
		        ps.setTimestamp(columnIndex, (Timestamp)value);
		      else if (valueType.equals(java.sql.Date.class))
		        ps.setTimestamp(columnIndex, new Timestamp(((java.sql.Date)value).getTime()));
		      else
		        ps.setObject(columnIndex, value);
		    }
		    else
		    {
		      ps.setObject(columnIndex, null);
		    }
		  }
	
	
	public List<Field> getIdFields(Class<?> clazz){
	    List<Field> fieldList = new ArrayList<Field>();
	    Field[] fields = clazz.getDeclaredFields();
	    for (Field f : fields) {
	      if (f.isAnnotationPresent(Id.class)) {
	        fieldList.add(f);
	      }
	    }
	    return fieldList;
	}
	
	public List<Field> getAllDBFields(Class<?> clazz){
		 
		List<Field> fieldList = new ArrayList<Field>();
	    Field[] fields = clazz.getDeclaredFields();
	    for (Field f : fields) {
	      if ((f.isAnnotationPresent(NotDbColumn.class)) || 
	        ("serialVersionUID".equalsIgnoreCase(f.getName()))) continue;
	      fieldList.add(f);
	    }

	    return fieldList;
	  }
	
	public Map<String, String> getCloumnNameMap(Class<?> clazz){
	    Map<String, String> mapNames = new HashMap<String, String>();
	    List<Field> fList = this.getAllDBFields(clazz);
	    for (Field f : fList) {
	      if (f.isAnnotationPresent(DbColumn.class)) {
	    	  DbColumn column = (DbColumn)f.getAnnotation(DbColumn.class);
	        if (!column.name().equalsIgnoreCase("fieldName")) {
	          mapNames.put(f.getName(), column.name());
	        }
	        else
	          mapNames.put(f.getName(), f.getName());
	      }
	      else
	      {
	        mapNames.put(f.getName(), f.getName());
	      }
	    }
	    return mapNames;
	  }
	
	
	public String getCloumnName(Class<?> clazz, Field f){
		return getCloumnNameMap(clazz).get(f.getName());
	}
	
	 public String getTableName(Class<?> clazz){
	    if (clazz.isAnnotationPresent(Table.class)) {
	      Table table = (Table)clazz.getAnnotation(Table.class);
	      if (!table.name().equalsIgnoreCase("className")) {
	        return table.name();
	      }
	    }
	    String name = clazz.getName();
	    return name.substring(name.lastIndexOf(".") + 1);
	  }
}

